library(ggpubr)
library(ggrepel)
library(patchwork)
library(clusterProfiler)
library(enrichplot)
library(tidyverse)

mast_tt_deg |>
  rownames_to_column('gene') |>
  plot_bill_volc(exp_group = 'TT') +
  ggtitle('TT vs II+IT mast cells DEG')

nk_tt_deg |>
  rownames_to_column('gene') |>
  plot_bill_volc('TT')+
  ggtitle('TT vs II+IT NK cells DEG')

main_tt_deg |>
  filter(cluster == 'Myeloid') |>
  plot_bill_volc('TT')+
  ggtitle('TT vs II+IT myeloid cells DEG')

main_tt_deg |>
  filter(cluster == 'CD4T') |>
  plot_bill_volc('TT')+
  ggtitle('TT vs II+IT CD4T cells DEG')

main_tt_deg |>
  filter(cluster == 'CD8T') |>
  plot_bill_volc('TT')+
  ggtitle('TT vs II+IT CD8T cells DEG')

main_tt_deg |>
  filter(cluster == 'B') |>
  plot_bill_volc('TT')+
  ggtitle('TT vs II+IT B cells DEG')

mast_tt_go_up <- main_tt_deg |>
  filter(cluster == 'Mast', avg_log2FC > 0, p_val_adj < .05) |> 
  pull(gene) |>
  enrichGO(OrgDb = 'org.Hs.eg.db',
           keyType = 'SYMBOL', ont = 'BP', readable = TRUE)

mast_tt_go_up@result |>
  filter(p.adjust < .05) |>
  plot_enrichment() +
  ggtitle('Mast cell upregulated pathways in TT')

mast_tt_go_down <- main_tt_deg |>
  filter(cluster == 'Mast', avg_log2FC < 1, p_val_adj < .05) |> 
  pull(gene) |>
  enrichGO(OrgDb = 'org.Hs.eg.db',
           keyType = 'SYMBOL', ont = 'BP', readable = TRUE)

mast_tt_go_down <- mast_tt_go_down |>
  pairwise_termsim()

mast_tt_go_down@result |>
  filter(p.adjust < .05) |>
  slice_min(p.adjust, n = 10) |>
  plot_enrichment('blue') +
  ggtitle('Mast cell downregulated pathways in TT') +
  theme(plot.title.position = 'plot')

mast_tt_go_down |>
  treeplot(offset.params = list(bar_tree = rel(3)),
           label_format_tiplab = 70)

nrow(mast_tt_go_down@result)

mast_tt_go_down_sim <- simplify(mast_tt_go_down)
nrow(mast_tt_go_down_sim@result)

mast_tt_go_down_sim@result |>
  filter(p.adjust < .05) |>
  slice_min(p.adjust, n = 10) |>
  plot_enrichment('blue') +
  ggtitle('Mast cell downregulated pathways in TT') +
  theme(plot.title.position = 'plot')

mast_tt_go_down_sim |>
  treeplot(offset.params = list(bar_tree = rel(3)),
           label_format_tiplab = 70)

mast_tt_go_down_sim |>
  emapplot(showCategory=10,
           cluster.params = list(label_format = 40, label_style = 'ggforce'),
           cex.params = list(category_label = .7))

